/**
 * 
 */
package com.avsa.contratacion.model.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import com.avsa.contratacion.model.Agricultor;
import com.avsa.contratacion.model.dao.AgricultorDAO;

public class JPAAgricultorDAO implements AgricultorDAO {

	private EntityManager em;
	
	public JPAAgricultorDAO(EntityManager em){
		this.em = em;
	}

	public List<Agricultor> findAll() {
		Query query=em.createQuery("Select o From Agricultor o order by o.rendimiento desc");
		return query.getResultList();
	}
	
	public Agricultor findById(int id) {
		return em.find(Agricultor.class, id);
		
	}

	
	public Agricultor find(Agricultor o) throws Exception {
		
		try{
			Query query=em.createQuery("Select o From Agricultor o where o.dni=?1");
			query.setParameter(1, o.getDni());
			return (Agricultor)query.getSingleResult();
		}catch(NoResultException e){
			return null;
		}
			
		
		
	}

	
	public Agricultor insert(Agricultor o) {
		// TODO Auto-generated method stub
		return null;
	}

	
	public void update(Agricultor o) {
		// TODO Auto-generated method stub
		
	}

	
	public void delete(Agricultor o) {
		// TODO Auto-generated method stub
		
	}
}